/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: CarModelServiceLevelRepository
 * Author:   weisj
 * Date:     2018-07-30 16:05
 * Description: CarModelServiceLevelRepository
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.apes.scm.masterdata.car.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.masterdata.car.model.CarModel;
import com.apes.scm.masterdata.car.model.CarModelServiceLevel;
import com.apes.scm.masterdata.car.model.CarServiceLevel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈CarModelServiceLevelRepository〉
 *
 * @author weisj
 * @create 2018-07-30
 * @since 1.0.0
 */
public interface CarModelServiceLevelRepository extends CustomRepository<CarModelServiceLevel,String> {
    @Query("SELECT T FROM CarServiceLevel T WHERE 1 = 1 and (id = :id or name like %:value%) ")
    Page<CarServiceLevel> find(@Param("id") String id, @Param("value") String value, Pageable pageable);

    @Query("SELECT T FROM CarModel T WHERE 1 = 1 and (id = :id or name like %:value%) ")
    Page<CarModel> findCarModel (@Param("id") String id, @Param("value") String value, Pageable pageable);

    CarModelServiceLevel findByCarModel(CarModel carModel);

    @Query("select new map(a.carServiceLevel.id as carServiceLevelId, a.carServiceLevel.name as carServiceLevelName," +
            "       to_char(a.lastUpdatedStamp, 'yyyy-mm-dd hh24:mi:ss.FF3') as lastUpdatedStamp," +
            "       a.creator as creatorId, a.createDate as createDate, a.carModel.id as carModelId, a.carModel.name as carModelName," +
            "       a.maintenanceMileage as maintenanceMileage, a.maintenanceInterval as maintenanceInterval)" +
            "  from CarModelServiceLevel a " +
            " where a.lastUpdatedStamp >= :times or a.id = :id" +
            " order by a.lastUpdatedStamp, a.carModel.id asc")
    List<Map> findCarModelServiceLevelsByTs(@Param("times") Date times, @Param("id") String id, Pageable pageable);

}
